﻿
var stage;
var portalCanvas;
(function(window) {

    function GameEnginer() {
        this.initialize();
    }

    var p = GameEnginer.prototype;

    var arraySpin = new Array();

    var matrixPortal;
    var cointainerSpin;


    var fpf;
    p.initialize = function() {
        portalCanvas = document.getElementById("portalCanvas");
        stage = new createjs.Stage(portalCanvas);
        cointainerSpin = new createjs.Container();
        fpf = new createjs.Text("-- fps", "bold 18px Arial", "#FFF");
        onJoinRoom();


        stage.enableMouseOver(20);
        createjs.Ticker.addEventListener("tick", tick);
        createjs.Ticker.setFPS(50);

    };

    function tick() {
        fpf.text = Math.round(createjs.Ticker.getMeasuredFPS()) + " fps";
        stage.update();
    }

    function joinRoom(roomId) {
        if (!PortalCommon.isSign) {
            PortalCommon.Message('Bạn cần đăng nhập để chơi !');
            return;
        }

        $('#lobbyPlay').hide();
        $('#gamPlay').show();
        PortalCommon.ChangeRoom(roomId);
        PortalCommon.GetJackInfo();
        
        matrixPortal = new PortalMatrix();
        if (arraySpin.length == 0) {
            for (var i = 0; i < 3; i++) {
                var firt = new Array();
                for (var j = 0; j < 5; j++) {
                    firt.push({ Id: PortalCommon.FloorSpell() });
                }
                arraySpin.push(firt);
            }
        }
        matrixPortal.init(arraySpin);
        stage.addChild(matrixPortal, cointainerSpin, fpf);

    }

    function outRoom() {
        if (PortalCommon.isBet)
            return;

        $('#lobbyPlay').show();
        $('#gamPlay').hide();
        $('#totalWin').html(PortalCommon.MoneyFont24(0));
        PortalCommon.ChangeRoom(-1);

        destroyStage();


    }

    function destroyStage() {

        createjs.Tween.removeAllTweens();
        matrixPortal = undefined;

        stage.removeAllChildren();
        delete matrixPortal;
        
    }

    

    function onJoinRoom() {

        $(portals.room100).unbind('click');
        $(portals.room100).click(function() {
            joinRoom(1);
        });
        
        $(portals.room1000).unbind('click');
        $(portals.room1000).click(function () {
            joinRoom(2);
        });

        $(portals.room10000).unbind('click');
        $(portals.room10000).click(function () {
            joinRoom(3);
        });
        
        $(portals.back).unbind('click');
        $(portals.back).click(function () {
            outRoom();
        });

        $(portals.spin).unbind('click');
        $(portals.spin).click(function () {
            if (!PortalCommon.isSign) {
                PortalCommon.Message('Bạn cần đăng nhập để chơi !');
                return;
            }
            if (PortalCommon.lineSelect == "") {
                PortalCommon.Message('Bạn chưa đặt cửa!');
                return;
            }
            
            if (PortalCommon.isBet)
                return;
            $(portals.spin).removeClass().addClass('spin_fix');
            bet();

        });

        $(portals.auto).unbind('click');
        $(portals.auto).click(function () {
            if (!PortalCommon.isSign) {
                PortalCommon.Message('Bạn cần đăng nhập để chơi !');
                return;
            }
            if (PortalCommon.lineSelect == "") {
              
                PortalCommon.Message('Bạn chưa đặt cửa!');
                return;
            }
            

            PortalCommon.isAuto = !PortalCommon.isAuto;

            if (PortalCommon.isAuto) {

                $(portals.auto).removeClass().addClass('auto_fix');
            } else {
                
                $(portals.auto).removeClass().addClass('auto');
            }

            if (PortalCommon.isBet)
                return;
                bet();
            
        });

    }

    function bet() {
        
        PortalCommon.isBet = true;
        PortalCommon.SetSpin(PortalCommon.lineSelect, function (result) {
            if (result != null) {
                $('#totalWin').html(PortalCommon.MoneyFont24(0));
               
                createjs.Tween.removeAllTweens();
                
                PortalCommon.UpdateTotaMoney(PortalCommon.TotalMoney - result.Data.Prize);
                matrixPortal = undefined;
                delete matrixPortal;
                stage.removeAllChildren();
                matrixPortal = new PortalMatrix();
                matrixPortal.init(arraySpin);
                matrixPortal.onSpin(result.Data.Items, result.Data);

                cointainerSpin.removeAllChildren();
                delete cointainerSpin;

                cointainerSpin = new createjs.Container();

                stage.addChild(matrixPortal, cointainerSpin);
                arraySpin = result.Data.Items;

                matrixPortal.OnRaise(finis);

            } else {
                PortalCommon.isBet = false;
                $(portals.spin).removeClass().addClass('spin');
            }
        });
  
    }

    function finis(result) {
         if (result.PrizeValue > 0) {
             PortalCommon.UpdateTotalWin(result.PrizeValue - result.PrizeGold);

             stage.addChild(new ParticalMatrix());
             if (result.PrizeGold > 0) {
                 createjs.Tween.get(this, { override: true }).wait(2000 * result.LineWin.length - 1).call(function () {
                     stage.removeAllChildren();
                     var goldDigger = new GoldDigger(result.PrizeGold, result.BonusGame);
                     goldDigger.OnRaise(function () {
                         destroyStage();
                         joinRoom(PortalCommon.RoomID);
                         $(portals.spin).removeClass().addClass('spin');
                         PortalCommon.isBet = false;
                     });
                     stage.addChild(goldDigger);
                     $('#RoomId').removeClass().addClass('play4');
                 });
             }
         
         } else {
             $(portals.spin).removeClass().addClass('spin');
             PortalCommon.isBet = false;
         }

         if (PortalCommon.isAuto) {
            bet();
        }  
    }

    //Effect
    window.GameEnginer = GameEnginer;

}(window));




